package com.zhengxyou.jetpackdemo.room;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.zhengxyou.jetpackdemo.R;
import com.zhengxyou.jetpackdemo.room.entity.User;
import com.zhengxyou.jetpackdemo.room.entity.UserInfo;

import java.util.Date;
import java.util.List;

import androidx.appcompat.app.AppCompatActivity;

public class RoomActivity extends AppCompatActivity {
    private TextView tv_createUser;
    private TextView tv_number;
    private int createMan = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_room);
        tv_createUser = findViewById(R.id.tv_createUser);
        tv_number = findViewById(R.id.tv_number);

        findViewById(R.id.btn_add).setOnClickListener(v -> {
            User user = new User();

            user.firstName = ++createMan + "_firstName";
            user.lastName = createMan + "lastName";
            user.birthday = new Date();
            user.cardId = "1324566545";
            tv_createUser.append(user.firstName + "\n");
//            Observable.just(1)
//                    .subscribeOn(Schedulers.io())
//                    .observeOn(Schedulers.io())
//                    .subscribe(new Consumer<Integer>() {
//                        @Override
//                        public void accept(Integer integer) throws Exception {
//                            AppDatabase.getInstance().userDao().getAll().size();
//                            runOnUiThread(new Runnable() {
//                                @Override
//                                public void run() {
//                                    tv_number.setText(String.valueOf(integer));
//                                }
//                            });
//                        }
//                    });
//            AppDatabase.getInstance().userDao().insertUser(user);
            new Thread(() -> {
                AppDatabase.getInstance().userDao().insertUser(user);
                List<User> all = AppDatabase.getInstance().userDao().getAll();
                if (all != null) {
                    UserInfo info = new UserInfo();
                    info.uid = all.get(0).id;
                    info.address = "sdasd";
                    AppDatabase.getInstance().userInfoDao().insert(info);
                    Log.e("TAG", all.get(0).birthday.getTime() + "");
                }
            }).start();
        });
    }

    public static void start(Context context) {
        Intent starter = new Intent(context, RoomActivity.class);
        context.startActivity(starter);
    }
}
